package edu.rit.varapp.utilities;

import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.TableModel;

/**
 *
 * @author Eric Kisner
 */
public class JTableUtils {

    /**
     * Returns all values of a JTable as a Collection of a collection of strings, which represents
     * a Collection of rows.
     * @param numRows The number of rows in the JTable.
     * @param numColumns The number of columns in the JTable.
     * @param table The JTable we want to extract data from.
     * @return A collection of 'rows'.
     */
    public static ArrayList<ArrayList<String>> dataFromTable( JTable table ) {
        TableModel m = table.getModel();
        int numRows = m.getRowCount();
        int numColumns = m.getColumnCount();
        ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>();
        for( int i = 0; i < numRows; i++ ) {
            ArrayList<String> row = new ArrayList<String>();
            for( int j = 0; j < numColumns; j++ ) {
                row.add( ((String) m.getValueAt( i, j )).trim() );
            }
            ret.add( row );
        }
        return ret;
    }

    /**
     * Returns all values of a JTable with a SINGLE row of data. This should only be used when the JTable is
     * known to have one row of information, and no more.
     * @see JTableUtils#dataFromTable(int, int, javax.swing.JTable)
     */
    public static ArrayList<String> singleDataFromTable( JTable table ) {
        TableModel m = table.getModel();
        int numColumns = m.getColumnCount();
        ArrayList<String> ret = new ArrayList<String>();
        for( int i = 0; i < 1; i++ ) {
            for( int j = 0; j < numColumns; j++ ) {
                ret.add( ((String) m.getValueAt( i, j )).trim() );
            }
        }
        return ret;
    }
}
